package com.lfm.park.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lfm.park.Service.TemporaryService;
import com.lfm.park.Service.UTemporaryService;
import com.lfm.park.model.Temporary;
import com.lfm.park.model.UTemporary;
import com.lfm.park.ret.RetResponse;
import com.lfm.park.ret.RetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lfm
 * @date 2020/3/17 - 11:53
 */
@RestController
@RequestMapping("/temp")
public class TemporaryController {
    @Autowired
    private TemporaryService temporaryService;
    @Autowired
    private UTemporaryService uTemporaryService;
    @GetMapping("/selectById")
    public Temporary selectById(Integer id){
        return temporaryService.selectById(id);
    }

    @RequestMapping("/selectAllTemporary")
    public RetResult<Object> selectAllTemporary(Temporary temporary, int page, int limit){
        PageInfo<Temporary> pageInfo=temporaryService.selectAllTemporary(temporary, page, limit);
        List<Temporary> list=pageInfo.getList();
        return RetResponse.makeOKRsp(list,pageInfo.getTotal());
    }
    @RequestMapping("/addTemporary")
    public RetResult<Object>addTemporary(Temporary temporary){
        temporary.setEntertime(new Date());
        temporary.setEndtime(new Date());
//        默认userid为1
        temporary.setUserid(1);
//        默认金额为100
        temporary.setMoney(100);
        temporaryService.addTemporary(temporary);
        Integer temporaryid=temporary.getId();
        addUT(temporaryid);
       return RetResponse.makeOKRsp();
    }
    public void addUT(Integer id){
        UTemporary uTemporary=new UTemporary();
        uTemporary.setUserid(1);
        uTemporary.setTemporaryid(id);
        uTemporaryService.addUt(uTemporary);
    }
    @RequestMapping("/updateTemporary")
    public RetResult<Object>updateTemporary(Temporary temporary){
        temporaryService.updateTemporary(temporary);
        return RetResponse.makeOKRsp();
    }

    @RequestMapping("/deleteTemporary")
    public RetResult<Object> deleteTemporary(@RequestBody String obj) {
        String[] str = obj.split(",");
        String id1 = str[0];
        String[] id2 = id1.split(":");
        Integer id3 = Integer.valueOf(id2[1]);
        Temporary temporary = new Temporary();
        temporary.setId(id3);
       temporaryService.deleteTemporary(temporary);
        return RetResponse.makeOKRsp();
    }

}
